<template>
    <div>
        <a-modal
            :title="modaObj.title"
            :visible="addzdwjVisible"
            :maskClosable="false"
            :confirm-loading="confirmLoading"
            width="60%"
            @ok="handleOk"
            @cancel="handleCancel"
            >
            <a-form-model
                ref="ruleForm"
                :model="form"
                :rules="rules"
                :label-col="labelCol"
                :wrapper-col="wrapperCol"
            >
                <a-form-model-item label="版本">
                    <a-input v-model="form.fbb" type="textarea"/>
                </a-form-model-item>
                <a-form-model-item label="文件简称" prop="fwjjc">
                    <a-input v-model="form.fwjjc" type="textarea"/>
                </a-form-model-item>
                <a-form-model-item label="文件全称" prop="fwjqc">
                    <a-input v-model="form.fwjqc" type="textarea"/>
                </a-form-model-item>
            </a-form-model>
        </a-modal>
    </div>
</template>
<script>
import {addZdwj,updateZdwj} from './service/index'
export default {
    props: {
        modaObj: {
            type: Object,
            default: false,
        }
    },
    data() {
        return {
            addzdwjVisible: false,
            confirmLoading: false,
            labelCol: { span: 6 },
            wrapperCol: { span: 14 },
            kongForm: {},
            form: {
                fbb: '',
                fwjjc: '',
                fwjqc: '',
            },
            rules: {
                fwjjc: [
                    { required: true, message: '请输入法规简称', trigger: 'blur' },
                ],
                fwjqc: [
                    { required: true, message: '请输入法规名称', trigger: 'blur' },
                ],
            },
        }
    },
    methods: {
        handleOk(){
            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    let data = {
                        fbb: this.form.fbb,
                        fwjjc: this.form.fwjjc,
                        fwjqc: this.form.fwjqc,
                        id: this.modaObj.formData?this.modaObj.formData.id:null
                    }
                    if(this.modaObj.formData){
                        updateZdwj(data).then(res => {
                            if(res.code == 200){
                                this.$message.success(res.message)
                                this.handleCancel()
                                this.$emit('success')
                            } else {
                                this.$message.warning(res.message)
                            }
                        })
                    } else {
                        addZdwj(data).then(res => {
                            if(res.code == 200){
                                this.$message.success(res.message)
                                this.handleCancel()
                                this.$emit('success')
                            } else {
                                this.$message.warning(res.message)
                            }
                        })
                    }
                }
            })
        },
        handleCancel(){
            this.addzdwjVisible = false
            this.form = {...this.kongForm}
        }
    },
    watch: {
        modaObj(newval,oldVal){
            //console.log(newval)
            this.kongForm = JSON.parse(JSON.stringify(this.form))
            if(newval.formData){
                this.form = {
                    ...newval.formData
                }
            }
        },
        deep:true
    },
}
</script>
